package com.star.percalservice.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MyDateUtil {

    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";


    /**
     * 获取当前日期
     * @return 返回字符串格式的当前日期
     */
    public static String getNowDate() {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        return format.format(new Date());
    }

    /**
     * 获取当前日期时间
     * @return 返回字符串格式的当前日期时间
     */
    public static String getNowDateTime() {
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
        return format.format(new Date());
    }

    /**
     * 获取指定天数前的日期
     * @param days
     * @return 返回字符串格式的指定天数前的日期
     */
    public static String getBeforeDate(int days) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        Date date = new Date();
        date.setTime(date.getTime() - (long) days * 24 * 60 * 60 * 1000);
        return format.format(date);
    }

    /**
     * 获取指定天数前的日期时间
     * @param days
     * @return
     */
    public static String getBeforeDateTime(int days) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
        Date date = new Date();
        date.setTime(date.getTime() - (long) days * 24 * 60 * 60 * 1000);
        return format.format(date);
    }

    /**
     * 获取上月的开始日期 格式：yyyy-MM-dd 00:00:00
     *
     * @return
     */
    public static String getLastMonthStartDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
        return format.format(calendar.getTime());
    }


    /**
     * 获取上月的结束日期 格式：yyyy-MM-dd 23:59:59
     *
     * @return
     */
    public static String getLastMonthEndDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
        return format.format(calendar.getTime());
    }

    /**
     * 获取最近n个月的月份列表
     * @param n
     * @return
     */
    public static List<String> getLastNMonths(int n) {
        String[] months = new String[n];
        Calendar now = Calendar.getInstance();
        int currentYear = now.get(Calendar.YEAR);

        for (int i = 0; i < n; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -i);

            int year = calendar.get(Calendar.YEAR);
            int monthNumber = calendar.get(Calendar.MONTH) + 1;
            String monthStr = monthNumber + "月";

            months[i] = (year != currentYear) ? year + "年" + monthStr : monthStr;
        }
        return List.of(months);
    }

    /**
     * 获取指定月份的开始日期
     * @param month
     * @return
     */
    public static String getMonthStartDate(String month) {
        try {
            // 解析输入格式（支持 "X月" 和 "XXXX年X月"）
            String[] parts = month.split("年|月");
            int year = Calendar.getInstance().get(Calendar.YEAR);
            int monthValue = 1;

            if (parts.length == 1) { // 格式：4月
                monthValue = Integer.parseInt(parts[0].replaceAll("\\D", ""));
            } else if (parts.length == 2) { // 格式：2024年12月
                year = Integer.parseInt(parts[0]);
                monthValue = Integer.parseInt(parts[1].replaceAll("\\D", ""));
            }

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthValue - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            return new SimpleDateFormat(DATE_TIME_FORMAT).format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取指定月份的结束日期
     * @param month
     * @return
     */
    public static String getMonthEndDate(String month) {
        try {
            // 复用解析逻辑
            String startDate = getMonthStartDate(month);
            if (startDate == null) return null;

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new SimpleDateFormat(DATE_TIME_FORMAT).parse(startDate));

            // 获取当月最后一天
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);

            return new SimpleDateFormat(DATE_TIME_FORMAT).format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
